Skip to content

Fix: whitelist own username in bio link detection - #52

Open
TayoBus wants to merge 2 commits into
rezhajulio:mainfrom
TayoBus:fix/bio-self-link-whitelist
Open

TayoBus wants to merge 2 commits into
rezhajulio:mainfrom
TayoBus:fix/bio-self-link-whitelist

Conversation

@TayoBus

@TayoBus TayoBus commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Problem

A user was hit by a false positive from the bio-link spam detector: their Telegram profile bio contained a link to their own profile (t.me/<their-username>), and has_suspicious_bio_links flagged it as a promo link because the function never knew whose bio it was checking.

Fix

Dynamic per-user self-whitelist in src/bot/handlers/bio_bait.py:

  • has_suspicious_bio_links(bio) now accepts an optional username parameter (the bio owner's Telegram username).
  • t.me/<own-username> / telegram.me/<own-username> links (any scheme, case-insensitive) are treated as self-references and skipped.
  • Bare @<own-username> mentions are excluded from the suspicious-mention count.
  • handle_bio_bait_spam passes user.username from the message sender through to the check.
  • The static community whitelists (WHITELISTED_TELEGRAM_PATHS, is_url_whitelisted) are untouched — the self-whitelist is dynamic per user and applies only to the bio check.

Review follow-ups (second commit)

The link exemption is deliberately narrow:

  • A leading @ is stripped when extracting the profile username, so the t.me/@username form is also recognized as a self-reference.
  • Only standalone matches are exempt: a t.me/<own-username> match embedded in a longer URL (evil.com/t.me/<own-username>) falls through to the normal whitelist check and is flagged as before.
  • Only the bare profile link is exempt: t.me/<own-username>/<subpath> no longer qualifies and is checked as usual.

Edge cases preserved

  • Users without a username (username=None): behavior unchanged.
  • Bio with own link plus another channel link: still flagged (only self-references are skipped).
  • Private invite links (t.me/+hash): always flagged — they can never be a self-reference.
  • Single @own-username + promo hint word: no longer flagged (mention count drops to 0).

Verification

  • ruff check: clean
  • mypy src/bot/handlers/bio_bait.py: clean
  • pytest tests/test_bio_bait.py: 118 passed (15 new tests covering the self-whitelist and the review follow-ups)
  • Full suite: 1373 passed

has_suspicious_bio_links now accepts the bio owner's username and treats
t.me/<own-username> links and @<own-username> mentions as self-references
instead of promo links. Fixes false positives for users whose bio links
to their own profile.
Review follow-ups:
- Strip a leading @ when extracting the profile username, so the
  t.me/@username form is recognized as a self-reference.
- Only exempt standalone matches: a t.me/<own-username> match embedded
  in a longer URL (evil.com/t.me/<own-username>) is checked as usual.
- Only exempt the bare profile link: t.me/<own-username>/<subpath>
  no longer qualifies and is checked as usual.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant